import axios from "axios";
import {Message} from "element-ui";
import {getToken} from "@/utils/auth";
import {logout} from "@/utils/login";

let request = axios.create()


request.interceptors.request.use(
    config => {
        if (getToken()) {
            config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
        }
        config.headers['Content-Type'] = 'application/json'
        return config
    },
    error => {
       return  Promise.reject(error)
    }
)

request.interceptors.response.use(response => {
        // console.log(response)
        return response
    },
    error => {
        // console.log(error)
        Message({
            message: error,
            type: 'warning'
        })
        // console.log(error.response.status)
        let code = error.response.status
        if (code === 401) {
            logout()
        }
        return Promise.reject(error)
    })

export default request
